package cn.iocoder.yudao.module.biz.controller.admin.specialWork;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.specialWork.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.specialWork.BizSpecialWorkDO;
import cn.iocoder.yudao.module.biz.convert.specialWork.BizSpecialWorkConvert;
import cn.iocoder.yudao.module.biz.service.specialWork.BizSpecialWorkService;

@Api(tags = "管理后台 - 工种")
@RestController
@RequestMapping("/biz/special-work")
@Validated
public class BizSpecialWorkController {

    @Resource
    private BizSpecialWorkService specialWorkService;

    @PostMapping("/create")
    @ApiOperation("创建工种")
    @PreAuthorize("@ss.hasPermission('biz:special-work:create')")
    public CommonResult<String> createSpecialWork(@Valid @RequestBody BizSpecialWorkCreateReqVO createReqVO) {
        return success(specialWorkService.createSpecialWork(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新工种")
    @PreAuthorize("@ss.hasPermission('biz:special-work:update')")
    public CommonResult<Boolean> updateSpecialWork(@Valid @RequestBody BizSpecialWorkUpdateReqVO updateReqVO) {
        specialWorkService.updateSpecialWork(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除工种")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:special-work:delete')")
    public CommonResult<Boolean> deleteSpecialWork(@RequestParam("id") String id) {
        specialWorkService.deleteSpecialWork(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得工种")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:special-work:query')")
    public CommonResult<BizSpecialWorkRespVO> getSpecialWork(@RequestParam("id") String id) {
        BizSpecialWorkDO specialWork = specialWorkService.getSpecialWork(id);
        return success(BizSpecialWorkConvert.INSTANCE.convert(specialWork));
    }

    @GetMapping("/list")
    @ApiOperation("获得工种列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:special-work:query')")
    public CommonResult<List<BizSpecialWorkRespVO>> getSpecialWorkList(@RequestParam("ids") Collection<String> ids) {
        List<BizSpecialWorkDO> list = specialWorkService.getSpecialWorkList(ids);
        return success(BizSpecialWorkConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/listByType")
    @ApiOperation("获得工种列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:special-work:query')")
    public CommonResult<List<BizSpecialWorkRespVO>> getSpecialWorkListByType(@RequestParam("typeId") String typeId) {
        List<BizSpecialWorkDO> list = specialWorkService.getSpecialWorkListByTypeId(typeId);
        return success(BizSpecialWorkConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得工种分页")
    @PreAuthorize("@ss.hasPermission('biz:special-work:query')")
    public CommonResult<PageResult<BizSpecialWorkRespVO>> getSpecialWorkPage(@Valid BizSpecialWorkPageReqVO pageVO) {
        PageResult<BizSpecialWorkDO> pageResult = specialWorkService.getSpecialWorkPage(pageVO);
        return success(BizSpecialWorkConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出工种 Excel")
    @PreAuthorize("@ss.hasPermission('biz:special-work:export')")
    @OperateLog(type = EXPORT)
    public void exportSpecialWorkExcel(@Valid BizSpecialWorkExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BizSpecialWorkDO> list = specialWorkService.getSpecialWorkList(exportReqVO);
        // 导出 Excel
        List<BizSpecialWorkExcelVO> datas = BizSpecialWorkConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "工种.xls", "数据", BizSpecialWorkExcelVO.class, datas);
    }

}
